import { query,add,remove,edit ,editStatus,resetPassword,queryOneUsergroup } from '@/services/applications';
import moment from 'moment'
export default {
  namespace: 'applications',
  state: {
    pageLoaded:false,
    data:[],
    scrollData:{x:0,y:0},
    selectedRowKeys: [],
    searchParams:{
      bank_id:{
        key:'',
        label:''
      },
      started_at:moment(new Date().getFullYear()+'-'+(parseInt(new  Date().getMonth())+1)+'-'+'01' , 'YYYY-MM-DD').format('YYYY-MM-DD'),
      ended_at: moment(new Date(), 'YYYY-MM-DD').format('YYYY-MM-DD'),
      license_plate_number:'',
      mobile:'',
      status:'0',
      page: 1,
      per_page: 30,
    },
    meta: {total: 0, per_page: 0,current_page:1},
    loading: true,
    editRecord:{}
  },
  effects: {
    *reset({ payload, callback }, { call, put }) {
     console.log('reset')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.applications)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.applications)//获取state中的数据
      payload=payload||state.searchParams;
      const response = yield call(query, payload);
      console.log(response)
      if(response.status===200){
        yield put({
          type: 'save',
          payload:  response.data
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }

    },
    *fetchOneusergroup({ payload ,callback}, { call, put }) {
      const response = yield call(queryOneUsergroup, payload);
      console.log(response)
      yield put({
        type: 'saveOneRecord',
        payload:  response.data
      });
      if(response.status===200){
        if (callback) callback();
      }
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);

      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }else{
      }
    },
    *edit({ payload, callback }, { call, put }) {
      console.log('edit')
      const response = yield call(edit, payload);
      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }
    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload,
      });
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.status===200){
        if (callback) callback();
      }
    },
    *exportCSV({ payload, callback }, { call, put }) {
      // const response = yield call(exportCSV, payload);
      // console.log(response)
      // if(response.status===200){
      //   if (callback) callback(response.data.download_key);
      // }
      callback();
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
        meta:action.payload.meta
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    saveAndPush(state, action) {
      // console.log('[...state.data,...action.payload.data]',[...state.data,...action.payload.data]);
      const data=[...state.data,...action.payload.data];
      // data.map((item,index)=>{
      //   item.index=index
      // })
      return {
        ...state,
        data: data,
        meta:action.payload.meta
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: action.payload,
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        data:[],
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        searchParams:{
          bank_id:{
            key:'',
            label:''
          },
          started_at:moment(new Date().getFullYear()+'-'+(parseInt(new  Date().getMonth())+1)+'-'+'01' , 'YYYY-MM-DD').format('YYYY-MM-DD'),
          ended_at: moment(new Date(), 'YYYY-MM-DD').format('YYYY-MM-DD'),
          license_plate_number:'',
          mobile:'',
          status:'0',
          page: 1,
          per_page: 30,
        },
        meta: {total: 0, per_page: 0,current_page:1},
        loading: true,
        editRecord:{}
      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
  },

};
